package com.karl.util;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

@SuppressWarnings("unchecked")
@Component("pageDao")
public class PageDao {

	private HibernateTemplate hibernateTemplate;
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	private String hql;
	private Page page = new Page();
	private int start;

	public void init() {
		
		this.start = 0;
		hql = "from Product";
		getRowCount();
		setTotalPage();
		setCurrentPage();
		setPrePage();
		setNextPage();
		setPreOrNextBoolean();

	}

	public int getRowCount() {
		List list = hibernateTemplate.find(hql);
		if (list.isEmpty())
			return 0;
		else
			return list.size();
	}

	public Page getPage() {

		List list = (List) hibernateTemplate.execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0) throws HibernateException, SQLException {

				Query query = (Query) arg0.createQuery(hql);
				query.setFirstResult(getStartIndex());
				query.setMaxResults(page.getPageSize());
				return query.list();
			}

		});

		page.setList(list);
		return page;
	}

	private int getStartIndex() {

		int startIndex = 0;
		if (start <= 0) {
			startIndex = 0;
		} else {
			if (start > page.getTotalPage()) {
				startIndex = page.getPageSize() * (page.getTotalPage() - 1);
			} else {
				startIndex = page.getPageSize() * (page.getCurrentPage() - 1);
//				startIndex = page.getPageSize() * (start - 1);
			}
		}
		return startIndex;
	//	currentRecord = startInex;
	}

	public void setPrePage() {
		page.setPrePage(page.getCurrentPage() - 1);
	}

	public void setNextPage() {
		page.setNextPage(page.getCurrentPage() + 1);
	}

	public void setRowCount() {
		page.setRowCount(getRowCount());
	}

	public void setTotalPage() {
		int rowCount = getRowCount();
		int pageSize = page.getPageSize();
		if (rowCount > pageSize) {
			if (rowCount % pageSize == 0) {
				page.setTotalPage(rowCount / pageSize);
			} else {
				page.setTotalPage(1 + (rowCount / pageSize));
			}
		} else {
			page.setTotalPage(1);
		}
	}

	public void setPreOrNextBoolean() {
		if (page.getCurrentPage() <= 1) {
			page.setHasPrePage(false);
		} else {
			page.setHasPrePage(true);
		}
		if (page.getCurrentPage() >= page.getTotalPage()) {
			page.setHasNextPage(false);
		} else {
			page.setHasNextPage(true);
		}
	}

	public void setCurrentPage() {
		if (start < 1) {
			page.setCurrentPage(1);
		}
		if (start > page.getTotalPage()) {
			page.setCurrentPage(page.getTotalPage());
		}
		page.setCurrentPage(start);
	}
	
	public void setPage(int index){
		page.setCurrentPage(index);
	}
}
